<template>
  <view class="container">
	  
    <!-- 页面标题 -->
    <view class="header">
      <text class="title">学生列表</text>
      <view class="action-buttons">
        <button type="primary" size="mini" @click="addStudent" class="edit">新增</button>
      </view>
    </view>


 <view class="notice_box" v-if="this.children_list.length ==0" >
 	<u-alert :title="title" type = "warning" effect="dark" :description = "description" @click="addStudent" ></u-alert>
 </view>
	 
	 
    <!-- 学生信息列表 -->
    <view class="student-list">
      <block v-for="(student, index) in children_list" :key="index">
        <view class="student-item">
			
          <view class="student-name">
            <text>{{ student.name }}</text>
          </view>
		  
		  <view class="student-info">
		    <text>{{ student.school }}</text>
		    <view>
			<text>{{ student.grade }}</text>
		    <text>( {{ student.class_ }} )</text>
			</view>
		  </view>
		  
		  
          <view class="action-buttons">
            <button type="primary" size="mini" @click="editStudent(student)" class="edit">修改</button>
            <button type="default" size="mini" :plain="true" @click="deleteStudent(student)">删除</button>
          </view>
        </view>
      </block>
    </view>

 
 <view class="joinbutton"v-if="this.children_list.length != 0" @tap="goto_order">
 	<view  class="join_btn" >立刻订奶</view>
 </view>
 

<!-- 没有数据时显示空数据组件 -->
	  <EmptyData v-if="this.children_list.length == 0" />
 
  </view>
</template>






<script>

import * as UserApi from '@/api/user'
	import EmptyData from '@/components/common/EmptyData.vue'
		
		
export default {
	
	components: {
		EmptyData
	},
	
  data() {
    return {
      children_list: [ ],
	  description:"请添加学生信息，用于订购牛奶。",
    };
  },
  onLoad(e) {
	  
	  			if (uni.getStorageSync('user_id')) {
	  				this.isLogin = true
	  
	  				console.log("登陆了",uni.getStorageSync('user_id'))
	  
	  			}
	  			else if (!this.isLogin) {
	  				uni.navigateTo({
	  					url: "/pages/login/index"
	  				}) 
	  				console.log("还没登陆")
	  			} 
	   
  	this.get_children_list()
  },
  onShow() 
  {
        console.log("触发刷新")
		this.get_children_list()
      },
	  
	  
  methods: {
    addStudent() {
      // 这里可以打开一个弹窗或跳转到新增页面
      console.log('新增学生');
	  uni.navigateTo({
	  	url:"/pages/user/student_add"
	  })
    },
	
	goto_order()
	{
  console.log("用户点击了立刻订奶");
  uni.switchTab({
    url: '/pages/activity/activity',
    success: () => {
      console.log("跳转成功");
    },
    fail: (err) => {
      console.error("跳转失败", err);
    }
  });
	},
	
	// 获取其所属的学生
	get_children_list() {
		// 创建一个参数对象，包含 nid 参数
				    const params = {
				        member_nid: uni.getStorageSync('user_id')
				    };
					
		UserApi.children_list(params).then(res => {
			this.children_list = res.info
			console.log(this.children_list,"this.children_list")
	 
		})
	},
    editStudent(student) {
      // 这里可以打开一个弹窗或跳转到编辑页面
      console.log('编辑学生:', student);
	  uni.navigateTo({
	  	url:`/pages/user/student_edit?nid=${student.nid}`
	  })
    },
	
	
	
	
	
	
    deleteStudent(student) {
      // 这里可以实现删除逻辑
      console.log('删除学生:', student);
	  
	  // 显示加载提示
	      uni.showLoading({
	          title: '正在删除',
	          mask: true // 防止用户操作
	      });
		  
		  
	  const params = {
	  		        child_nid: student.nid
	  		    };
	  			
	  			
	  UserApi.children_delete(params).then(res => {
		  
		  // 隐藏加载提示
		          uni.hideLoading();
	  	this.children_delete = res.info
		
	  	this.get_children_list()
	  }).catch(error => {
        // 隐藏加载提示
        uni.hideLoading();

        // 处理错误
        console.error("删除学生失败", error);
        uni.showToast({
            title: '删除失败，请重试',
            icon: 'none'
        });
    });
	  
	  
    }
  }
};
</script>



<style scoped lang="scss">
	@import url("asset/style.css");
</style>
 